var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    refCode: null,
    toast1Hidden: true,
    modalHidden: true,
    modalHidden2: true,
    notice_str: '',
  },
  //表单提交
  formSubmit: function (e) {
    var that = this;
        wx.showLoading({
          title: '加载中',
        })

        var myreg = /^[0-9a-zA-Z]+$/;

          let inputCode = e.detail.value.code;
          if (inputCode.length == 0) {
            wx.showToast({
              title: "邀请码不能为空!",
              icon: 'none',
              duration: 2000
            });
            return false;
          }else if (!myreg.test(inputCode)) {
            wx.showToast({
              title: '邀请码格式有误！',
              icon: 'none',
              duration: 1500
            })
            return false;
          }
          util.request(api.bindByCode + inputCode).then(function (res) {
            console.log("userInfo:" + JSON.stringify(res));
            app.globalData.userInfo = res;
            wx.hideLoading();
            console.log("userInfo:" + JSON.stringify(res));
            wx.setStorageSync('userInfo', res);
            wx.showToast({
              title: '绑定成功',
              icon: 'success',
              duration: 2000
            })
            wx.navigateBack()  //返回上级页面
          }).catch(function (res) {
            console.log("获取数据失败！")
            wx.hideLoading();

          })
  },
  formReset: function () {
    this.setData({
      modalHidden2: false,
      isAgree: false
    });
    wx.showToast({
      title: '清空成功！',
      icon: 'none'
    })
  },

  //弹出确认框  
  modalTap: function (e) {
    this.setData({
      modalHidden: true
    })
  },
  confirm_one: function (e) {
    console.log(e);
    this.setData({
      modalHidden: true,
      toast1Hidden: false,
      notice_str: '提交成功'
    });
  },
  cancel_one: function (e) {
    console.log(e);
    this.setData({
      modalHidden: true,
      toast1Hidden: false,
      notice_str: '取消成功'
    });
  },
  //弹出提示框  
  modalTap2: function (e) {
    this.setData({
      modalHidden2: false
    })
  },
  modalChange2: function (e) {
    this.setData({
      modalHidden2: true
    })
  },

  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})